package com.cicada.cocos2dsamples.samples;

import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCParallaxNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCSpriteSheet;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.view.MotionEvent;

public class GameBackground extends BaseActivity {

	@Override
	public CCLayer getLayer() {
		return new Layer();
	}

	class Layer extends CCLayer {
		static final int tagNode = 1;
		CGSize winSize;

		public Layer() {
			winSize = CCDirector.sharedDirector().displaySize();
			setIsTouchEnabled(true);
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();

			cache.addSpriteFrames("SnowScene-mw.plist");
			CCSpriteSheet.spriteSheet("SnowScene-mw.png");

			CCParallaxNode voidNode = CCParallaxNode.node();

			CCSprite layer1 = CCSprite.sprite(cache
					.getSpriteFrame("level2-layer1-mw.png"));
			layer1.setAnchorPoint(CGPoint.make(0, 0));
			layer1.setScale(2.0f);

			CCSprite layer2 = CCSprite.sprite(cache
					.getSpriteFrame("level2-layer2-mw.png"));
			layer2.setAnchorPoint(CGPoint.make(0, 0));
			layer2.setScale(1.5f);

			CCSprite layer3 = CCSprite.sprite(cache
					.getSpriteFrame("level2-layer3-mw.png"));
			layer3.setAnchorPoint(CGPoint.make(0, 0));

			CCSprite layer_4l = CCSprite.sprite(cache
					.getSpriteFrame("level2-layer-4L-mw.png"));
			layer_4l.setAnchorPoint(CGPoint.make(0, 0));

			CCSprite layer_4r = CCSprite.sprite(cache
					.getSpriteFrame("level2-layer-4R-mw.png"));
			layer_4r.setAnchorPoint(CGPoint.make(0, 0));

			voidNode.addChild(layer1, 1, 0.3f, 1.0f, 0, winSize.getHeight()
					- layer1.getContentSize().height);
			voidNode.addChild(layer2, 2, 0.33f, 1.0f, 0, 0);
			voidNode.addChild(layer3, 3, 0.5f, 1.0f, 0, 0);
			voidNode.addChild(layer_4l, 4, 1.0f, 1.0f, 0, 0);
			voidNode.addChild(layer_4r, 4, 1.0f, 1.0f,
					layer_4l.getContentSize().width, 0);
			addChild(voidNode, 0, tagNode);
		}

		CGPoint previousLocation = new CGPoint();

		@Override
		public boolean ccTouchesBegan(MotionEvent event) {
			previousLocation.set(event.getX(), event.getY());
			return CCTouchDispatcher.kEventHandled;
		}

		@Override
		public boolean ccTouchesEnded(MotionEvent event) {
			return CCTouchDispatcher.kEventHandled;
		}

		@Override
		public boolean ccTouchesCancelled(MotionEvent event) {
			return CCTouchDispatcher.kEventIgnored;
		}

		@Override
		public boolean ccTouchesMoved(MotionEvent event) {
			CGPoint diff = CGPoint.zero();

			CGPoint touchLocation = CGPoint.make(event.getX(), event.getY());

			CGPoint location = CCDirector.sharedDirector().convertToGL(
					touchLocation);
			CGPoint prevLocation = CCDirector.sharedDirector().convertToGL(
					previousLocation);

			diff.x = location.x - prevLocation.x;
			// diff.y = location.y - prevLocation.y;

			CCNode node = getChildByTag(tagNode);
			node.setPosition(CGPoint.ccpAdd(node.getPosition(), diff));
			previousLocation = touchLocation;

			return CCTouchDispatcher.kEventHandled;
		}
	}
}
